package com.example.message.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.message.entity.User;
import com.example.message.entity.Task1;
import com.example.message.service.SysUserService;
import com.example.message.service.Task1Service;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequiredArgsConstructor
@RequestMapping("/task")
public class TaskController {

	private final Task1Service taskService;
	private final SysUserService sysUserService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param task 数据源表
	 * @return
	 */
	@GetMapping("/page" )
	public R getTaskConfPage(Page page, Task1 task,@RequestParam Integer loginId) {
		//管理员登陆id为1，控制管理员查看所有
		if(loginId != 1){
			task.setUserId(loginId);
		}
		return R.ok(taskService.page(page, Wrappers.lambdaQuery(task).orderByAsc(Task1::getIsFinish)
				.orderByDesc(Task1::getCreateTime).orderByAsc(Task1::getLevel)));
	}


	/**
	 * 通过id查询数据源表
	 * @param id id
	 * @return R
	 */
	@GetMapping("/{id}" )
	public R getById(@PathVariable("id" ) Integer id) {
		return R.ok(taskService.getById(id));
	}

	/**
	 * 新增数据源表
	 * @param task 数据源表
	 * @return R
	 */
	@PostMapping
	public R save(@RequestBody Task1 task,@RequestParam Integer loginId,@RequestParam String username) {

//		int userId = SecurityUtils.getUser().getId();
//		String userName = SecurityUtils.getUser().getUsername();

		task.setUserId(loginId);
		task.setUserName(username);
		task.setCreateTime(new Date());

		if(task.getLevel().equals(null) || task.getLevel().equals("")){
			task.setLevel("1");
		}

		System.out.println("新增task:"+task.toString());
		return R.ok(taskService.save(task));
	}

	/**
	 * 修改数据源表
	 * @param task 数据源表
	 * @return R
	 */
	@PutMapping
	public R updateById(@RequestBody  Task1 task) {
		if (task.getIsFinish() == 1){
			task.setProgress(100);
		}
		if(task.getProgress() <100){
			task.setIsFinish(0);
		}else if(task.getProgress() == 100){
			task.setIsFinish(1);
		}
		return R.ok(taskService.updateById(task));
	}

	/**
	 * 通过id删除数据源表
	 * @param id id
	 * @return R
	 */
	@DeleteMapping("/{id}")
	public R removeById(@PathVariable Integer id) {
		return R.ok(taskService.removeById(id));
	}

	/**
	 * 通过任务id获取分配的成员的任务信息
	 * @param
	 * @return R
	 */
	@GetMapping("/getTaskUserById/{taskId}" )
	public R getTaskUserById(Page page,@PathVariable Integer taskId) {
		return R.ok(taskService.getTaskById(page,taskId));
	}

	/**
	 * 分配任务新增
	 * @param
	 * @return R
	 */
	@PostMapping("/addTaskUser/{parent}" )
	public R addTaskUser(@RequestBody Task1 task1,@PathVariable("parent") Integer parent) {
//		String username = sysUserService.getUserVoById(task1.getUserId()).getUsername();
		System.out.println(parent);

		User u = new User();
		u.setId(task1.getUserId());
		User u2 = sysUserService.getOne(new QueryWrapper<>(u));

		System.out.println(u2);
		task1.setUserName(u2.getUsername());
		task1.setParent(parent);
		task1.setCreateTime(new Date());
		if(task1.getLevel().equals(null) || task1.getLevel().equals("")){
			task1.setLevel("1");
		}
		return R.ok(taskService.save(task1));
	}

	/**
	 * 获取部门成员
	 * @param
	 * @return R
	 */
	@GetMapping("/getAllUser" )
	public R getAllUser() {
//		String deptId = SecurityUtils.getUser().getDeptId().toString();
		return R.ok(sysUserService.getAllUser());
	}

	/**
	 * 分配任务修改
	 * @param
	 * @return R
	 */
	@PutMapping("/updateTaskUser/{parent}" )
	public R updateTaskUser(@RequestBody Task1 task1,@PathVariable("parent") Integer parent) {

//		String username = sysUserService.getUserVoById(task1.getUserId()).getUsername();
		User u = sysUserService.getById(task1.getUserId());

		task1.setUserName(u.getUsername());
		task1.setParent(parent);
		if (task1.getIsFinish() == 1){
			task1.setProgress(100);
		}
		if(task1.getProgress() <100){
			task1.setIsFinish(0);
		}else if(task1.getProgress() == 100){
			task1.setIsFinish(1);
		}
		return R.ok(taskService.updateById(task1));
	}

	/**
	 * 分配任务删除
	 * @param
	 * @return R
	 */
	@DeleteMapping("/deleteTaskUser/{id}" )
	public R deleteTaskUser(@PathVariable Integer id) {
		return R.ok(taskService.removeById(id));
	}




}
